<?php
/**
 * Introduction: 商品库存表  stock_supplie_products
 */

namespace Common\Model;


use Think\Model;

class StockSupplieProductsModel extends Model
{
    //定义数据库连接信息
    protected $connection;

    //定义表名
    protected $tableName = 'stock_supplie_products';

    /**
     * 初始化
     */
    public function _initialize()
    {

        $this->connection = C('Databaese');

    }

    /**
     * 获取库存
     * @param unknown $skuId
     * @param unknown $supplieid
     * @return int
     */
    public function getStock($skuId, $supplieid)
    {
        $field = ['stock'];
        $record = $this->where("supplieid=$supplieid and product_sku_id=$skuId and status>0")->field($field)->find();
        $stock = 0;
        if (!empty($record)) {
            $stock = $record['stock'];
        }
        return $stock;
    }

    /**
     * 更新商品库存
     * @param unknown $skuId
     * @param unknown $num
     * @param unknown $supplierId
     */
    public function updateStocks($skuId, $num, $supplierId)
    {
        $info = $this->where("product_sku_id=$skuId and supplieid=$supplierId")->field('stock')->find();
        $res = $this->where("product_sku_id=$skuId and supplieid=$supplierId")->save(['stock'=>($info['ctock']-$num)]);

        return $res?true:false;
    }

}